package com.karaku.karaku_collector.util.screenshot;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;

import com.karaku.karaku_collector.App;
import com.karaku.karaku_collector.util.actions.Action2;

public class ScreenShotActivity extends Activity {

    private static Action2<Integer, Intent> callback;

    public static void start(Action2<Integer, Intent> callback) {
        ScreenShotActivity.callback = callback;
        Intent i = new Intent(App.getInstance(), ScreenShotActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        App.getInstance().startActivity(i);
    }

    private int result = 0;
    private Intent data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            MediaProjectionManager m = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
            startActivityForResult(m.createScreenCaptureIntent(), 0);
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        this.result = resultCode;
        this.data = data;
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (callback != null) {
            callback.run(result, data);
            callback = null;
        }
    }

}
